package concurrent.thread.pool.executor;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Hashtable;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2021-05-30 16:34:00
 */
@Slf4j
public class BaseTast {

    /**
     * 解决多层循环嵌套
     *  - 在第一层循环里面某一个层循环中break，那程序直接break第一层循环
     *  - 在第一层循环里面某一个层循环中continue，那程序直接continue第一层循环
     * @see java.util.concurrent.ThreadPoolExecutor#addWorker(Runnable, boolean)
     */
    @Test
    public void test01(){
        Random random = new Random();
        retry:
        for (;;) {
            int i = random.nextInt(2);
            log.info("" + i);
            for (;;) {
                if (i == 1){
                    log.info("break");
                    break retry;
                }else{
                    log.info("retry");
                    continue retry;
                }
            }
        }
    }

    /**
     * @see java.util.concurrent.ScheduledThreadPoolExecutor#schedule(Runnable, long, TimeUnit)
     *  - 执行一次延迟任务
     * @see java.util.concurrent.ScheduledThreadPoolExecutor#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
     *  - 本次执行结束，延迟，再执行
     * @see java.util.concurrent.ScheduledThreadPoolExecutor#scheduleAtFixedRate(Runnable, long, long, TimeUnit)
     *  - 到点执行，若当前任务未执行完，下一次执行任务的时间到了，不会并发执行，下次任务会延迟执行，要等到当前任务执行完毕后再执行
     */
    @Test
    public void test02() throws InterruptedException {
        Random random = new Random();
        /*
            18:54:49.454 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:2
            18:54:51.478 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:0
            18:54:53.380 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:3
            18:54:56.383 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:2
            18:54:58.389 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:0
            18:54:59.378 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:3
            18:55:02.379 [pool-1-thread-1] INFO concurrent.thread.pool.executor.BaseTast - 固定速率执行任务:0
         */
        ScheduledFuture<?> scheduledFuture = new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(() -> {
            int i = random.nextInt(4);
            log.info("固定速率执行任务:" + i);
            try {
                TimeUnit.SECONDS.sleep(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 0, 2, TimeUnit.SECONDS);
        Thread.currentThread().join();
    }

    /**
     * 守护线程、用户线程
     * 当不存在守护线程时，jvm会正常退出
     * main运行后，jvm会启动一个DestroyJavaVM的线程(守护线程)，该线程会等待所有用户线程结束后终止JVM进程
     * @param args
     */
    public static void main(String[] args) {
        Runnable r = () -> {
//            while (true){
//
//            }
            for (int i = 0; i < 10; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    log.info(String.valueOf(i));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread thread = new Thread(r);
        thread.setDaemon(true); //设置未守护线程
        thread.start();
        System.out.println("ok~");
    }

}
